﻿#include "itemedit.h"
#include "ui_itemedit.h"

ItemEdit::ItemEdit(int id, QWidget *parent) :
    _id(id),
    QDialog(parent),
    ui(new Ui::ItemEdit)
{
    ui->setupUi(this);

    initView();
}

ItemEdit::~ItemEdit()
{
    delete ui;
}

void ItemEdit::initView()
{
    QSqlQuery query;
    QString sql=QString("select * from %1 where id=%2").arg(TD_PRODUCT).arg(_id);
    query.exec(sql);
    if(query.next())
    {
        QString category=query.value("category").toString();
        QString label=query.value("label").toString();
        QString detail=query.value("detail").toString();
        QString addition=query.value("addition").toString();

        ui->category->setText(category);
        ui->label->setText(label);
        ui->detail->setPlainText(detail);
        ui->addition->setPlainText(addition);
    }
}

void ItemEdit::on_btn_save_clicked()
{
    QString category=ui->category->text().trimmed();
    if(category.isEmpty())
        return;

    QString label=ui->label->text().trimmed();
    if(label.isEmpty())
        return;

    QString detail=ui->detail->toPlainText();
    if(detail.isEmpty())
        return;

    QString addition=ui->addition->toPlainText();

    QString sql=QString("update %1 set category = '%2',label='%3',detail='%4',addition='%5' where id=%6")
            .arg(TD_PRODUCT)
            .arg(category)
            .arg(label)
            .arg(detail)
            .arg(addition)
            .arg(_id);

    QSqlQuery query;
    if(!query.exec(sql))
        QMessageBox::warning(this,QStringLiteral("异常情况"),QStringLiteral("无法保存！"));
    else
        this->accept();
}
